package com.queue.rabbitmq.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitmqConfig {

    /**
     * 单队列
     *
     * @return
     */
    @Bean
    public Queue queueWork1() {
        return new Queue("queue_work");
    }

    /**
     * 发布订阅模式
     *
     * @return
     */
    // 声明两个队列
    @Bean
    public Queue queueFanout1() {
        return new Queue("queue_fanout1");
    }

    @Bean
    public Queue queueFanout2() {
        return new Queue("queue_fanout2");
    }

    //准备一个交换机
    @Bean
    public FanoutExchange exchangeFanout() {
        return new FanoutExchange("exchange_fanout");
    }

    //交换机和队列绑定
    @Bean
    public Binding bindingExchange1() {
        return BindingBuilder.bind(queueFanout1()).to(exchangeFanout());
    }

    //交换机和队列绑定
    @Bean
    public Binding bindingExchange2() {
        return BindingBuilder.bind(queueFanout2()).to(exchangeFanout());
    }

    /**
     * topic 模型
     *
     * @return
     */
    @Bean
    public Queue queueTopic1() {
        return new Queue("queue_topic1");
    }

    @Bean
    public Queue queueTopic2() {
        return new Queue("queue_topic2");
    }

    //准备一个topic交换机
    @Bean
    public TopicExchange exchangeTopic() {
        return new TopicExchange("exchange_topic");
    }

    /**
     * 注意key规则
     * 其中"*"用于匹配一个单词， "#"用于匹配多个单词(可以是零)
     * 匹配imooc.* 表示可以匹配 imooc.hello ,但是imooc.hello.one需要使用imooc.#才可以匹配到
     *
     * @return
     */
    //交换机和队列绑定#是绑定多个单词
    @Bean
    public Binding bindingTopic1() {
        return BindingBuilder.bind(queueTopic1()).to(exchangeTopic()).with("topic.#");
    }

    //交换机和队列绑定*是绑定一个单词
    @Bean
    public Binding bindingTopic2() {
        return BindingBuilder.bind(queueTopic2()).to(exchangeTopic()).with("topic.*");
    }

    /**
     * 测试confirm 机制，专门创建了一个队列
     */
    @Bean
    public Queue queueConfirm() {
        return new Queue("queue_confirm");
    }

    /**
     * 测试return机制
     *
     * @return
     */
    @Bean
    public Queue queueReturn() {
        return new Queue("queue_return");
    }

    @Bean
    public TopicExchange exchangeReturn(){
        return new TopicExchange("exchange_return");
    }
    @Bean
    public Binding bindingReturn(){
        return BindingBuilder.bind(queueReturn()).to(exchangeReturn()).with("return.*");
    }

    /**
     * TTL队列   （Time To Live）
     * RabbitMQ允许为消息和队列设置TTL（生存时间），若对消息设置了ttl，如果超过了ttl配置则消息死了,称之为死信.请注意
     */
    @Bean
    public Queue delayTTLQueue(){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("x-message-ttl",10000);
        //注明死信队列的交换机以及route_key
        paramMap.put("x-dead-letter-exchange","exchange_dlx");
        paramMap.put("x-dead-letter-routing-key","receive_key");
        return new Queue("queue_ttl",true,false,false,paramMap);
    }
    /**
     * 死信队列
     */
    @Bean
    public Queue queueDLX(){
        Map<String,Object> map = new HashMap<>(2);
        map.put("x-message-ttl",5000);
        //注明死信队列接收的交换机以及recevice_key
        map.put("x-dead-letter-exchange","exchange_receive");
        map.put("x-dead-letter-routing-key","receive_key");
        return  new Queue("queue_dlx",true,false,false,map);
    }
    @Bean
    public DirectExchange exchangeDLX(){
        return new DirectExchange("exchange_dlx");
    }
    @Bean
    public Binding bindingDLX(){
        return BindingBuilder.bind(queueDLX()).to(exchangeDLX()).with("receive_key");
    }

    /**
     * 死信接收交换机
     * @return
     */
    @Bean
    public DirectExchange exchangeReceive(){
        return new DirectExchange("exchange_receive");
    }

    /**
     * 接收死信的队列
     * @return
     */
    @Bean
    public Queue queueReceive(){
        return new Queue("queue_receive");
    }
    @Bean
    public Binding bindingReceive(){
        return BindingBuilder.bind(queueReceive()).to(exchangeReceive()).with("receive_key");
    }
}
